package com.ibix.servelt;

import com.ibix.dao.UserDao;
import com.ibix.daoImpl.UserDaoImpl;
import com.ibix.entity.Users;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

import java.io.PrintWriter;
import java.util.List;



public class ServletUsers extends HttpServlet {
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /*----------------------------- 所有的请求全部都会走这个方法啊---------------------*/

    /* response：响应 的作用是使客户端浏览器，区分不同种类的数据，并根据不同的MIME调用浏览器内不同的程序嵌入模块来处理相应的数据。*/
    /**
     *   text/html; charset=utf-8    html
     *   text/plain; charset=utf-8 文本
     *   text/javascript   json数据
     *   application/xml   xml数据
     */

    /*request：请求 设置HTTP 响应的编码，用于设置服务器给客户端的数据的编码*/

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        /*request.getSession() 等同于 request.getSession(true) 没有的话会创建另外一个 */
        HttpSession session = request.getSession();
        /* 拿到需要输出的流 */
        PrintWriter out = response.getWriter();

        UserDao ud = new UserDaoImpl();

        /* 获取到请求的参数 */
        String dos = request.getParameter("do");
        System.out.print("----------------------       " + dos + "    -----------------\n");
        if (dos == null || dos.equals("")) {
            dos = "index";
        }
        // 主页
        if (dos.equals("index")) {
            // 得到数据库里面所有的User
            List<Users> ulist = ud.getAllUsers();
            //将数据保存好  将来发送给jsp文件使用
            session.setAttribute("ulist", ulist);
            response.sendRedirect("index.jsp");
            return;
        }
        if (dos.equals("add")) {
            String name = request.getParameter("name");
            String ages = request.getParameter("age");
            int age = Integer.parseInt(ages);
            Users u = new Users(0, name, age);
            ud.addUser(u);
            out.print("<script>alert('新增成功！');window.location='servletusers?do=index';</script>");
        }
        if (dos.equals("del")) {
            String ids = request.getParameter("id");
            int id = Integer.parseInt(ids);
            ud.delUser(id);
            out.print("<script>alert('删除成功！');window.location='servletusers?do=index';</script>");
            return;
        }
        if (dos.equals("editbefore")) {
            String ids = request.getParameter("id");
            int id = Integer.parseInt(ids);
            Users u=ud.findUserById(id);
            session.setAttribute("edituser", u);
            response.sendRedirect("edit.jsp");
            return;
        }
        if (dos.equals("edit")) {
            String ids=request.getParameter("id");
            int id=Integer.parseInt(ids);
            String name = request.getParameter("name");
            String ages = request.getParameter("age");
            int age = Integer.parseInt(ages);
            Users u = new Users(id, name, age);
            ud.updateUser(u);
            out.print("<script>alert('修改成功！');window.location='servletusers?do=index';</script>");
            return;
        }
    }
}
